$(function () {
    console.log('表哥我准备好查找了哦')
  
    $('#btnQuery').click(function () {
      console.log('你查了一次')
      let keyword = $('[name=keyword]').val()
      let obj = {
        keyword,
      }
      $.post('/crud/query', obj, function (res) {
        console.log(res);
        $('.tbRow').html('')
        res.forEach(item=>{
          let html=`
          <tr class="tbRow">
                        <td>${item.Id}</td>
                        <td>${item.ProductName}</td>
                        <td>${item.Supplier}</td>
                        <td>${item.Price}</td>
                        <td>${item.Stock}</td>
                        <td>${item.Remarks}</td>
                        <td>
                            <input class="btnEdit" onclick="btnEdit(${item.Id})" type="button" value="编辑">
                            <input class="btnDel" onclick="btnDel(${item.Id})" type="button" value="删除">
                        </td>
                    </tr>
          `
          
          $('#tbData').append(html)
        })
      })
    })
  
    $('#btnAdd').click(function () {
      window.location.href = `/crud/createoredit`
    })
  

  
    $('#btnSave').click(function () {
      let id = $('[name=id]').val()
      let productname = $('[name=ProductName]').val()
      let supplier = $('[name=supplier]').val()
      let price = $('[name=price]').val()
      let stock = $('[name=stock]').val()
      let remarks = $('[name=remarks]').val()
      let obj = {
        id,
        productname: productname,
        supplier,
        price,
        stock,
        remarks,
      }
      console.log(obj)
    
      $.post('/crud/save', obj, function () {
        alert('保存成功！')
        window.location.href = `/crud/index`
      })
    })
    $('#btnCancel').click(function () {
      window.location.href = `/crud/index`
    })
  })
  function btnEdit(id) {
    window.location.href = `/crud/createoredit/${id}`
  }
  
  function btnDel(id) {
    let msg = '你怎么敢？'
    if (confirm(msg)) {
      window.location.href = `/crud/Delete/${id}`
    } else {
      console.log('不敢了吧')
    }
  }